package com.hummingbird.portal.controller;


import com.hummingbird.portal.dto.RegisterDTO;
import com.hummingbird.portal.exception.ServiceException;
import com.hummingbird.portal.r.R;
import com.hummingbird.portal.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.UUID;

/**
 * 页面的访问
 */
@RestController
@Slf4j  //日志  log
public class SystemController {

    @Value("${hummingbird.resource.path}")
    private  String path;
    @Value("${hummingbird.resource.host}")
    private String host;

    @Autowired
    private IUserService userService;
    @GetMapping("/login.html")
    public ModelAndView  loginForm(){
        ModelAndView  mav = new ModelAndView("login");  //结合thymeleaf    对应的是/templates/login.html
        return mav;
    }


    @GetMapping("/register.html")
    public  ModelAndView register(){
        ModelAndView  mav = new ModelAndView("register");
        return mav;
    }
    @GetMapping("/upload.html")
    public ModelAndView  uploadForm(){
        ModelAndView  mav = new ModelAndView("upload");  //结合thymeleaf    对应的是/templates/login.html
        return mav;
    }

    @PostMapping("/register")
    public R registerStudent(@RequestBody @Validated RegisterDTO registerDTO, BindingResult bindingResult){
        //BindingResult   -- 保存验证信息的
        if(bindingResult.hasErrors()){
            return R.unproecsableEntity(bindingResult.getFieldError().getDefaultMessage());
        }
        if(!registerDTO.getPassword().equals(registerDTO.getConfirm())){
            return R.unproecsableEntity("两次密码不一致");
        }
        log.info("注册信息:{}",registerDTO);  //在日志输出端  打印   注册信息:XXXXX
        //System.out.println("注册信息:"+registerDTO);
        try{
            userService.registerStudent(registerDTO);
        }catch(Exception e){
            log.error("注册失败:{}",e);
            return R.failed(e);
        }
        return R.created("注册成功");
    }

    @PostMapping("/upload")
    public R uploadFile(MultipartFile imageFile) throws IOException {//多文件上传MultipartFile[]

        String datePath = DateTimeFormatter.ofPattern("yyyy/MM/dd").format(LocalDate.now());
        //SimpleDateFormat
     /*   SimpleDateFormat  sdf = new SimpleDateFormat("yyy/MM/dd");
        String str = sdf.format(new Date()); // 2424年11月9日    2024/11/09*/

        File  folder  = new File(path,datePath);//  E:/upload/2024/11/09--string
        if(!folder.exists()){
            folder.mkdirs();//E:/upload/2024/11/09---directory
        }
        String originalFilename  = imageFile.getOriginalFilename();//获取文件名  (包含扩展名)
        //改变原有文件名
        String ext = originalFilename.substring(originalFilename.lastIndexOf("."));//.jpg
        String newName = UUID.randomUUID().toString()+ext;

        File file = new File(folder,newName);//  E:/upload/a.txt
        imageFile.transferTo(file);//上传
        String url = host + "/"+datePath+"/"+newName;//http://localhost:9999/2024/11/09/sdfd.jpg
        return R.ok(url);
    }


}
